﻿package com.wandian.view.mediator
{
	import com.greensock.TweenLite;
	import com.wandian.controller.*;
	import com.wandian.data.GameData;
	import com.wandian.model.*;
	import com.wandian.model.vo.*;
	import com.wandian.util.CLZTiming;
	import com.wandian.util.GlowFilterManage;
	import com.wandian.view.components.ui.Person;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;

	/**
	* @author long
	* @QQ 108232706
	* @version 1.0
	*/
	public class SeatMediator extends Mediator
	{
		private var _arrSit:Array;
		private var _arrASit:Array;
		private var _arrInvitation:Array;
		private var _global:GameData;
		private var _inRoomData:InRoomPlayerDataProxy;
		private var _arrPerson:Array;
		private var _filter:GlowFilterManage;
		public static const NAME:String="SeatMediator";

		public function SeatMediator(param1:Object=null)
		{
			this._global=GameData.getInstance();
			this._filter=GlowFilterManage.getInstance();
			super(NAME, param1);
			return;
		}

		override public function onRegister():void
		{
			this._arrSit=[this.mcSeat.sit1, this.mcSeat.sit2, this.mcSeat.sit3, this.mcSeat.sit4, this.mcSeat.sit5, this.mcSeat.sit6, this.mcSeat.sit7, this.mcSeat.sit8, this.mcSeat.sit9];
			this._arrASit=[this.mcSeat.asit1, this.mcSeat.asit2, this.mcSeat.asit3, this.mcSeat.asit4, this.mcSeat.asit5, this.mcSeat.asit6, this.mcSeat.asit7, this.mcSeat.asit8, this.mcSeat.asit9];
			this._arrInvitation=[this.mcSeat.invitation1, this.mcSeat.invitation2, this.mcSeat.invitation3, this.mcSeat.invitation4, this.mcSeat.invitation5, this.mcSeat.invitation6, this.mcSeat.invitation7, this.mcSeat.invitation8, this.mcSeat.invitation9];
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			this.checkRoomNumSit();
			this._arrPerson=[];
			var count:int=0;
			while (count < this._arrSit.length)
			{

				this._arrSit[count].addEventListener(MouseEvent.CLICK, this.onSitHandler);
				this._arrPerson[count]=new Person();
				this._arrPerson[count].mouseChildren=false;
				this._arrPerson[count].buttonMode=true;
				this._arrPerson[count].addEventListener(MouseEvent.MOUSE_OVER, this.overHandler);
				this._arrPerson[count].addEventListener(MouseEvent.MOUSE_OUT, this.outHandler);
				this._arrPerson[count].addEventListener(MouseEvent.CLICK, this.clickHandler);
				this.mcSeat.addChild(this._arrPerson[count]);
				this.mcSeat.removeChild(this._arrInvitation[count]);
//				this._arrInvitation[count].visible=false;
				count=count + 1;
			}
			return;
		}

		/**
		 * 玩家点坐下按钮，线判断身上钱是否够
		 * @param event
		 */
		private function onSitHandler(event:MouseEvent):void
		{
//			if (this._global.judgeMoneyType(GameData._objRoom.type))
//			{
			if (this._global.userInfo.userVo.money < GameData._objRoom.minBlind)
			{
				this.sendNotification(CLZNoticeName.SHOW_BUY_VIP_PANEL_TIP);
				return;
			}
//			}
			var seatId:*=int(String(event.currentTarget.name).substring(3, 4));
			if (this._inRoomData.isEmpty)
			{
				GameData.lastSeat=seatId;
				var roomid:int=GameData._objRoom.id;
				trace("[seatmediator]第一个坐下的人，设置房间基础信息,roomid:" + roomid);
				this.sendNotification(CLZNoticeName.SET_ROOM_WINDOS_SHOW, {roomid: roomid}); //点坐位,显示买入
			}
			else
			{
				this.allProhibitClick();
				this.sendNotification(CLZNoticeName.BUY_IN_WINDOS_SHOW, {seatId: seatId, againBuyIn: 0}); //点坐位,显示买入	
			}

			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.LOBBY_UPDATEROOMVO, CLZNoticeName.SETROOMINFO_RSP, CLZNoticeName.RESPONSE_ROOM_INFO_SUCC, CLZNoticeName.SITDOWN_RSP, CLZNoticeName.STAND_UP_RSP, CLZNoticeName.GAME_OVER, CLZNoticeName.SITDOWN_ERR, CLZNoticeName.BUY_IN_WINDOS_CLOSE, CLZNoticeName.LOAD_PERSON_COMPLETE_BY_ID];
		}

		override public function handleNotification(notification:INotification):void
		{
			var data:Object=null;
			var playerVo:PlayerVo=null;
			var person:Person=null;
			var _loc_5:int=0;
			var icon:int=0;
			var index:int=0;
			data=notification.getBody() as Object;
			switch (notification.getName())
			{
				case CLZNoticeName.RESPONSE_ROOM_INFO_SUCC: //设置房间状态
				{
					this.checkRoomNumSit();
					break;
				}
				case CLZNoticeName.LOBBY_UPDATEROOMVO: //设置房间状态
				{
					this.checkRoomNumSit();
					break;
				}
				case CLZNoticeName.SETROOMINFO_RSP: //设置房间状态
				{
					this.checkRoomNumSit(true);
					trace("上次点了座位：" + GameData.lastSeat + "，目前座位：" + this._global.seatId);
					if (GameData.lastSeat > 0 && this._global.seatId == 0)
					{
						//检查坐位是否存着,顺时针下一个
						this.sendNotification(CLZNoticeName.BUY_IN_WINDOS_SHOW, {seatId: GameData.lastSeat, againBuyIn: 0}); //点坐位,显示买入
						GameData.lastSeat=0;
					}
					break;
				}
				case CLZNoticeName.SITDOWN_RSP: //第一次，坐下响应
				{
					playerVo=this._inRoomData.getInRoomPlayer(data.userid) as PlayerVo;
					this._arrPerson[(playerVo.seatId - 1)].showPersonById(playerVo.icon, playerVo.seatId);
					this._arrPerson[(playerVo.seatId - 1)].seatId=playerVo.seatId;
					if (data.userid == this._global.userInfo.userVo.userid)
					{
//						trace("当前玩家坐下,seatid："+data.seatId);
//						this._global.seatId=data.seatId;
						this.allProhibitClick();
					}
					else
					{
						this.prohibitClick(data.seatId);
					}
					this.checkRoomNumSit();
					break;
				}
				case CLZNoticeName.STAND_UP_RSP: //起来响应
				{
					person=this._arrPerson[(data.seatId - 1)];
					person.seatId=0;
					while (person.numChildren > 0)
					{

						person.removeChildAt(0);
					}
					if (data.seatId == this._global.seatId)
					{
						this._global.seatId=0;
						this.allRecoveryClick();
					}
					else if (this._global.seatId == 0)
					{
						this.recoveryClick(data.seatId);
					}
					this.checkRoomNumSit();
					break;
				}
				case CLZNoticeName.GAME_OVER: //游戏结束
				{
					index=1;
					while (index < 10)
					{

						playerVo=this._inRoomData.getSitPlayerBySeatId(index) as PlayerVo; //游戏结束，遍历9个坐位，若当前账户金额不够，自动站起
						if (playerVo.userid != 0)
						{
							if (playerVo.stake == 0)
							{
								if (playerVo.userid == this._global.userInfo.userVo.userid)
								{
									if (this._global.judgeMoneyType(GameData._objRoom.type))
									{
										if (this._global.userInfo.userVo.money < GameData._objRoom.minBlind)
										{
											this.sendNotification(CLZNoticeName.STAND_UP);
											this.sendNotification(CLZNoticeName.SHOW_BUY_VIP_PANEL_TIP);
											return;
										}
									}
									this.sendNotification(CLZNoticeName.BUY_IN_WINDOS_SHOW, {seatId: playerVo.seatId, againBuyIn: 1});
								}
								playerVo.lastAction=GameData.WAIT_BUY_IN;
								this.sendNotification(CLZNoticeName.UPDATE_GAME_PLAYER_LASTACTION, playerVo);
							}
							else
							{
								playerVo.againBuyIn=0;
							}
						}
						index=index + 1;
					}
					break;
				}
				case CLZNoticeName.AGAIN_SIT_BUYIN_RES:
				{
					if (data.errId > 0)
					{
						this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {});
					}
					break;
				}
				case CLZNoticeName.SITDOWN_ERR: //坐下错误
				{
					_loc_5=int(data);
					switch (_loc_5)
					{
						case -3:
						{
							data={okNoticeName: CLZNoticeName.RELOAD_WINDOW_PAGE, tip: GameData.tip["SIT_ERR_1"], tipTitle: GameData.tip["SYSTEM_TIP"], isCancel: false};
							this.sendNotification(CLZNoticeName.SHOW_WINDOWS_TIP, data);
							break;
						}
						case -4:
						{
							this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.tip["SIT_ERR_2"]});
							break;
						}
						case -5:
						{
							this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.tip["SIT_ERR_3"]});
							break;
						}
						case -6:
						{
							this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.tip["SIT_ERR_4"]});
							break;
						}
						default:
						{
							break;
						}
					}
					break;
				}
				case CLZNoticeName.BUY_IN_WINDOS_CLOSE:
				{
					this.allRecoveryClick();
					break;
				}
				case CLZNoticeName.LOAD_PERSON_COMPLETE_BY_ID:
				{
					icon=data as int;
					index=0;
					trace("update ico "+icon);
					while (index < this._inRoomData.sitAllPlayer.length)
					{

						playerVo=this._inRoomData.sitAllPlayer[index] as PlayerVo;
						if (playerVo != null && playerVo.userid > 0)
						{
							trace("update userid ico "+playerVo.icon );
							if (int(playerVo.icon) == icon)
							{
								this._arrPerson[(playerVo.seatId - 1)].updatePersonById(icon, playerVo.seatId);
							}
						}

						index++;
					}
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function checkRoomNumSit(checkBuyInSeat:Boolean=false):void
		{
			var arrSeat:Array=GameData.getRoomSit(GameData._objRoom.seat);
			var total:int=arrSeat.length;
			var index:int=0;
			while (index < 9)
			{

				this._arrASit[index].visible=false;
				this._arrSit[index].visible=false;
				index=index + 1;
			}
			var index2:int=0;
			while (index2 < total)
			{

				this._arrASit[(arrSeat[index2] - 1)].visible=true;
				this._arrSit[(arrSeat[index2] - 1)].visible=true;
				index2=index2 + 1;
			}
			if (checkBuyInSeat && GameData.lastSeat > 0)
			{
				var find:Boolean=false;
				trace("开始找坐位," + GameData.lastSeat);
				while (!find)
				{
					for (var i:int=0; i < arrSeat.length; i++)
					{
						if (arrSeat[i] == GameData.lastSeat)
						{
							find=true;
							break;
						}
					}
					if (!find)
					{
						GameData.lastSeat++;
						trace("坐位右移一位,到" + GameData.lastSeat);
						if (GameData.lastSeat > 9)
						{
							GameData.lastSeat=1;
						}
					}
				}
			}
			return;
		}

		/**
		 * 点了一个座位，禁止在点其他的坐位
		 */
		private function allProhibitClick():void
		{
			var index:int=0;
			while (index < 9)
			{

				this.prohibitClick((index + 1));
				index=index + 1;
			}
			return;
		}

		private function allRecoveryClick():void
		{
			if (this._global.seatId > 0)
			{
				return;
			}
			var index:int=0;
			while (index < 9)
			{

				if (this._inRoomData.getSitPlayerBySeatId((index + 1)).seatId == 0)
				{
					this.recoveryClick((index + 1));
				}
				index=index + 1;
			}
			return;
		}

		/**
		 * 设置坐位点击按钮状态
		 * @param seatId
		 */
		private function prohibitClick(seatId:int):void
		{
			this._arrSit[(seatId - 1)].mouseEnabled=false;
			return;
		}

		private function recoveryClick(param1:int):void
		{
			this._arrSit[(param1 - 1)].mouseEnabled=true;
			return;
		}

		private function get mcSeat():Object
		{
			return viewComponent;
		}

		private function overHandler(event:MouseEvent):void
		{
			var person:*=event.target as Person;
			person.filters=[this._filter.getHightLight()];
			CLZTiming.add(0.8, this.sendNotice, CLZNoticeName.OVER_PLAYER, person.seatId);
			return;
		}

		private function sendNotice(param1:String, param2:int):void
		{
			this.sendNotification(param1, param2);
			return;
		}

		private function outHandler(event:MouseEvent):void
		{
			CLZTiming.remove(this.sendNotice);
			var _loc_2:*=event.target as Person;
			_loc_2.filters=null;
			this.sendNotification(CLZNoticeName.OUT_PLAYER, _loc_2.seatId);
			return;
		}

		private function clickHandler(event:MouseEvent):void
		{
			var person:*=event.target as Person;
			var point:*=new Point();
			var data:*=new Object();
			switch (person.seatId)
			{
				case 1:
				{
					point.x=669.45;
					point.y=76.4;
					break;
				}
				case 2:
				{
					point.x=657;
					point.y=151.4;
					break;
				}
				case 3:
				{
					point.x=680;
					point.y=320;
					break;
				}
				case 4:
				{
					point.x=684.45;
					point.y=390.4;
					break;
				}
				case 5:
				{
					point.x=518.45;
					point.y=390.4;
					break;
				}
				case 6:
				{
					point.x=345.45;
					point.y=390.4;
					break;
				}
				case 7:
				{
					point.x=166.45;
					point.y=342.4;
					break;
				}
				case 8:
				{
					point.x=168.45;
					point.y=138.45;
					break;
				}
				case 9:
				{
					point.x=341.45;
					point.y=57.45;
					break;
				}
				default:
				{
					break;
				}
			}
			data.point=point;
			data.seatId=person.seatId;
			this.sendNotification(CLZNoticeName.BTN_PLAYER_OPERATE, data);
			return;
		}

	}
}
